include make-apptools-include.mk

CFLAGS="-DSVDVERSION=\"2.0\"" "-DCOMPDATE=\"`date`\"" -g

all:	
	@echo "Specify either tosvd, fstool, or fquery."
	@exit 2

tester: $(OBJ)/tester.o

dmktest: $(OBJ)/floppy.o $(OBJ)/dmk.o $(OBJ)/dmktest.o $(OBJ)/svd.o

$(OBJ)/tosvd.o:	

$(OBJ)/floppy.o: floppy.c floppy.h

$(OBJ)/svd.o: svd.c svd.h

$(OBJ)/svd12.o: svd12.c svd.h 

$(OBJ)/svd15.o: svd15.c svd.h

$(OBJ)/svd20.o: svd20.c svd20.h svd.h

SVDOBJS=$(OBJ)/svd.o $(OBJ)/svd12.o $(OBJ)/svd15.o $(OBJ)/svd20.o

$(OBJ)/fs-trs80.o: fs-trs80.c fs-trs80.h

$(OBJ)/fs-trs8034.o: $(OBJ)/fs-trs80.o fs-trs8034.c fs-trs8034.h

STANDOBJS = $(OBJ)/crc.o $(OBJ)/floppy.o $(OBJ)/format_init.o

###########################################################
#              F L O P P Y    F O R M A T S
###########################################################
#  These tools can support various different floppy input
#  formats such as DMK, JV3, etc.  To add a new format, do
#  the following:
#
# 1 - create a format code base that has the following:
#     (where you replace the * with the format name)
#
#       *_init() - initialization routine, this routine
#                  normally only calls the registration
#	           routine.
#
#        The *_init() routine must be named as shown
#        because this makefile generates code that calls
#        all of the floppy format init routines.
#
#    *_guesser() - a routine that will "guess" if the
#		   input file is of this format.  Some
#		   formats do a full read checking for
#                  errors...some just do a quick check.
#     *_reader() - the routine that fills up the floppy
#                  structure with the data from the read
#                  of the input file.
#     *_writer() - writes out the format...most routines
#                  don't actually supply one of these.
#   *_reporter() - generates a report of this format.
#
#        Note that these routines can be named whatever
#        you like in that the init routine calls the
#        registration routine to register the format.
#
# 2 - Update this makefile to inclue the format and the
#     file(s) that implement it.
#
#    FLOPPY_FORMAT_LIST - update this variable with your
#          new format name.  The order of this list is important.
#          The listed formats will be added in the given
#          order.  Therefore, the guesser routines will be
#          called in this order.  Note, too, that the format
#          name must then be the same as used for *_init().
#          So for the format "dmk" there must be a dmk_init().
#          The "user visible" name of the format is specified
#          in the *_init() routine.
#
#    FLOPPY_FORMAT_SRC - update this variable with the name of
#          the format source file(s) in any order.
###########################################################

# FLOPPY_FORMAT_LIST = dmk svd h17 h17v1 jvc jv3 jv1 generic generic2
FLOPPY_FORMAT_LIST = dmk svd h17 h17v1 jvc jv3 do po nib rnib t99 pc99 jv1 h8d
#FLOPPY_FORMAT_OBJS = $(OBJ)/dmk.o $(SVDOBJS) $(OBJ)/jv.o $(OBJ)/h17.o $(OBJ)/generic.o
FLOPPY_FORMAT_OBJS = $(OBJ)/dmk.o $(SVDOBJS) $(OBJ)/jv.o $(OBJ)/h17.o $(OBJ)/gcrmap.o \
		     $(OBJ)/gcrrevmap.o $(OBJ)/apple.o $(OBJ)/appleutil.o $(OBJ)/ti.o

format_init.c:
	@echo "building format_init.c..."
	@( COUNT=`echo "$(FLOPPY_FORMAT_LIST)" | wc -w`; \
	   echo "/* THIS FILE AUTOMATICALLY GENERATED */"; \
	   echo "#include <stdio.h>"; \
	   for f in $(FLOPPY_FORMAT_LIST); do echo "extern void $${f}_init();"; done; \
	   echo "typedef void (*fnptr)(void);"; \
	   echo "fnptr floppy_format_init_list[] = {"; \
	   for f in $(FLOPPY_FORMAT_LIST); do echo "$${f}_init,"; done; \
	   echo "NULL };"; ) > format_init.c

#
#
# The following two variables: FS and FSO
#  are used to indicate to make which file systems
#  to compile into the fstool.  FS includes the basename
#  that is used to refer to the filesystem, and FSO refers
#  to the objects.  This file assumes that each of the files
#  in FSO depends upon fs.h and an associated fs-*.h file
#  mirroring the FSO listed name.  The functions in the .o
#  should be named appropriately too, and are built into
#  the files as well.  Note that the first fs listed is
#  the default.
#
#  The "generate" target builds the include file that maps
#   the functions for the different fs's to arrays that can
#   be used for dispatch.
#

$(OBJ)/fs-%.o : fs-%.c fs-%.h
	$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

FS  = trs80 trs8034 cpm hcpm hdos coco ados33
FSO = $(OBJ)/fs-trs80.o $(OBJ)/fs-trs8034.o $(OBJ)/fs-cpm.o $(OBJ)/fs-hcpm.o $(OBJ)/fs-hdos.o $(OBJ)/fs-coco.o $(OBJ)/fs-ados33.o
FSLIBS = CPM/$(OBJ)/libcpm.a HDOS/$(OBJ)/libhdos.a CoCo/$(OBJ)/libcoco.a ADOS/$(OBJ)/libados.a

fssupport.c:
	  @echo "building fssupport.c"
	  @( echo "/* THIS FILE AUTOMATICALLY GENERATED */"; \
	  echo "#include \"fssupport.h\""; \
	  echo ;\
	  echo "int fs_count = " `echo $(FS) | wc -w` ";"; \
	  echo "char *fs_types[] = {"; for i in $(FS); do echo "\"$$i\","; done ; echo "\"\"};" ;\
	  echo ;\
	  echo "static enum fs_type fs_map(char *name) {"; \
	  for i in $(FS); do echo "if (strcmp(name,\"$$i\")==0) return($$i);"; done; echo "return(unknown);}";\
	  echo ;\
	  echo "int fs_init(struct floppy *floppy, struct fs *fs, char *name) {" ;\
	  echo "fs->fs_type = fs_map(name);" ;\
	  echo "switch(fs->fs_type) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_init(floppy,&fs->fs_union.$$i));";\
	     done ; echo "case unknown: return(0); }}"; \
	  echo ;\
	  echo "int fs_cleanup(struct fs *fs) {" ;\
	  echo "switch(fs->fs_type) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_cleanup(&fs->fs_union.$$i));";\
	     done ; echo "case unknown: return(0); }}"; \
	  echo ;\
	  echo "char  *fs_description(char *name) {" ;\
	  echo "switch(fs_map(name)) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_description());";\
	     done ; echo "case unknown: return(0); }}"; \
	  echo ;\
	  echo "int fs_add(struct fs *fs,char *name, int fd) {" ;\
	  echo "switch(fs->fs_type) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_add(&fs->fs_union.$$i,name,fd));";\
	     done ; echo "case unknown: return(0); }}"; \
          echo ;\
	  echo "int fs_del(struct fs *fs,char *name) {" ;\
	  echo "switch(fs->fs_type) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_del(&fs->fs_union.$$i,name));";\
	     done ; echo "case unknown: return(0); }}"; \
          echo ;\
	  echo "int fs_report(struct fs *fs,int verbosity) {" ;\
	  echo "switch(fs->fs_type) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_report(&fs->fs_union.$$i,verbosity));";\
	     done ; echo "case unknown: return(0); }}"; \
          echo ;\
	  echo "int fs_extract(struct fs *fs, char *name, int fd) {" ;\
	  echo "switch(fs->fs_type) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_extract(&fs->fs_union.$$i,name,fd));";\
	     done ; echo "case unknown: return(0); }}"; \
          echo ;\
	  echo "int fs_compress(struct fs *fs) {" ;\
	  echo "switch(fs->fs_type) {";\
	  for i in $(FS); do echo "case $$i:  return(fs_$${i}_compress(&fs->fs_union.$$i));";\
	     done ; echo "case unknown: return(0); }}"; \
          echo ;\
	  ) > fssupport.c
	  @echo "building fssupport.h"
	  @( echo "/* THIS FILE AUTOMATICALLY GENERATED */"; \
	  for i in $(FS); do echo "#include \"fs-$$i.h\""; done ;\
	  echo ;\
	  echo "union fs_union {" ;\
	  for i in $(FS); do echo "struct fs_$$i $$i;"; done ; echo "};" ;\
	  echo ;\
	  echo "enum fs_type {"; for i in $(FS); do echo "$$i,"; done ; echo "unknown};" ;\
	  echo ;\
	  echo "struct fs {"; \
	  echo "   enum fs_type fs_type;"; \
	  echo "   union fs_union fs_union;"; \
	  echo "};"; \
	  echo ;\
	  echo "extern char *fs_types[];";\
	  echo "extern int fs_count;";\
	  echo "extern int fs_init(struct floppy *, struct fs *, char *);";\
	  echo "extern int fs_cleanup(struct fs *);";\
	  echo "extern char *fs_description(char *);";\
	  echo "extern int fs_add(struct fs *, char *, int);";\
	  echo "extern int fs_del(struct fs *, char *);";\
	  echo "extern int fs_report(struct fs *, int);";\
	  echo "extern int fs_extract(struct fs *, char *, int);";\
	  echo "extern int fs_compress(struct fs *);";\
	  ) > fssupport.h

fstool: $(OBJ)/fssupport.o $(FSO) $(STANDOBJS) $(OBJ)/fstool.o $(OBJ)/fs-utility.o genrevmap $(FLOPPY_FORMAT_OBJS)
	(cd CPM; $(MAKE))
	(cd HDOS; $(MAKE))
	(cd CoCo; $(MAKE))
	(cd ADOS; $(MAKE))
	$(CC) -o $@ $(OBJ)/fssupport.o $(FSO) $(STANDOBJS) $(OBJ)/fstool.o $(OBJ)/fs-utility.o $(FLOPPY_FORMAT_OBJS) $(FSLIBS)
	rm -f $(OBJ)/fssupport.o fssupport.[ch]
	rm -f $(OBJ)/format_init.o format_init.c


tosvd: $(OBJ)/tosvd.o $(STANDOBJS) genrevmap $(FLOPPY_FORMAT_OBJS)
	$(CC) -o $@ $(OBJ)/tosvd.o $(STANDOBJS) $(FLOPPY_FORMAT_OBJS)
	rm -f $(OBJ)/format_init.o format_init.c

fquery: $(OBJ)/fquery.o $(STANDOBJS) $(FLOPPY_FORMAT_OBJS)
	$(CC) -o $@ $(OBJ)/fquery.o $(STANDOBJS) $(FLOPPY_FORMAT_OBJS)
	rm -f $(OBJ)/format_init.o format_init.c

genrevmap: $(OBJ)/gcrmap.o $(OBJ)/genrevmap.o
	gcc -o genrevmap $(OBJ)/genrevmap.o $(OBJ)/gcrmap.o

$(OBJ)/gcrrevmap.o: genrevmap
	echo "/* AUTO GENERATED */" > gcrrevmap.c
	echo "unsigned char gcr6x2revmap[] = {" >> gcrrevmap.c
	genrevmap 6 >> gcrrevmap.c
	echo "};"  >> gcrrevmap.c
	echo >> gcrrevmap.c
	echo "unsigned char gcr5x3revmap[] = {" >> gcrrevmap.c
	genrevmap 5 >> gcrrevmap.c
	echo "};"  >> gcrrevmap.c
	echo "int gcr6x2revmap_size = sizeof(gcr6x2revmap);"   >> gcrrevmap.c
	echo "int gcr5x3revmap_size = sizeof(gcr5x3revmap);"   >> gcrrevmap.c
	echo "extern unsigned char gcr6x2revmap[];" > gcrrevmap.h
	echo "extern unsigned char gcr5x3revmap[];" >> gcrrevmap.h
	echo "extern int gcr6x2revmap_size;" >> gcrrevmap.h
	echo "extern int gcr5x3revmap_size;" >> gcrrevmap.h
	gcc -c -o $(OBJ)/gcrrevmap.o gcrrevmap.c

clean:
	(cd CPM; $(MAKE) clean)
	(cd HDOS; $(MAKE) clean)
	(cd CoCo; $(MAKE) clean)
	(cd ADOS; $(MAKE) clean)
	rm -f *.o
	rm -f gcrrevmap.[ch]
	rm -f fstool fstool.exe
	rm -f tosvd tosvd.exe
	rm -f fquery fquery.exe
	rm -f dmktest dmktest.exe
	rm -f tester tester.exe
	rm -f fssupport.h fssupport.c
	rm -f format_init.c
	rm -f $(WINOBJDIR)/*
	rm -f $(LINOBJDIR)/*

realclean:	clean
	(cd CPM; $(MAKE) realclean)
	(cd HDOS; $(MAKE) realclean)
	(cd CoCo; $(MAKE) realclean)
	(cd ADOS; $(MAKE) realclean)
	rm -f *~
	rm -f ,out*


DISTDIR = Distribution
BACKUP = Backup

BUNDLENAME = app22
BUNDLECOMMENT = SVD CP Helper Apps v2.2 - Jun 2005

MAIN_CFILES = 	fquery.c fstool.c tosvd.c \
		floppy.c crc.c \
		fs-trs8034.c fs-ados33.c fs-utility.c fs-coco.c \
		fs-cpm.c fs-hcpm.c fs-hdos.c fs-trs80.c \
		jv.c ti.c h17.c dmk.c svd.c svd12.c svd15.c svd20.c \
		apple.c appleutil.c gcrmap.c gcrrevmap.c genrevmap.c

MAIN_HFILES =	crc.h floppy.h standard.h error.h  \
		h17.h svd20.h jv.h dmk.h svd.h \
		apple.h gcrmap.h gcrrevmap.h \
		fs-cpm.h fs-trs8034.h fs-ados33.h fs-hcpm.h fs-utility.h \
		fs-apple.h fs-hdos.h fs-coco.h fs-trs80.h

MISC = Makefile Makefile.windows make-apptools-include.mk

SRCFILES =	$(MAIN_CFILES) $(MAIN_HFILES) $(MISC)

windowsdist:	dist
	$(MAKE) -f Makefile.windows bundle

linuxdist:		dist
	$(MAKE) -f Makefile.linux bundle

dist: 
	[ -d $(DISTDIR) ] || mkdir $(DISTDIR)
	for file in $(SRCFILES); do ln $$file $(DISTDIR)/$$file; done
	(cd CPM; $(MAKE) "DISTDIR=../$(DISTDIR)/CPM" dist)
	(cd HDOS; $(MAKE) "DISTDIR=../$(DISTDIR)/HDOS" dist)
	(cd CoCo; $(MAKE) "DISTDIR=../$(DISTDIR)/CoCo" dist)
	(cd ADOS; $(MAKE) "DISTDIR=../$(DISTDIR)/ADOS" dist)
